{% extends "smErp/base.html" %}

{% block css_style %}
    {% load staticfiles %}
    <link href="{% static 'smErp/css/brand_info.css' %}" type="text/css" rel="stylesheet">
    <link href="{% static 'smErp/assets/dropzone/basic.min.css' %}" type="text/css" rel="stylesheet">
    <link href="{% static 'smErp/assets/dropzone/dropzone.min.css' %}" type="text/css" rel="stylesheet">

{% endblock %}



{% block content %}
    <div class="inner wrp">

        <div class="page_nav">
            <a href="{% url 'smErp:manage' %}" class="jsUrlLink" data-url="{% url 'smErp:manage' %}" target="_self">管理中心</a>
            <span class="gap">/</span>
            <a href="{% url 'smErp:product_info' product.id %}" class="jsUrlLink" data-url="{% url 'smErp:product_info' product.id %}" target="_self">产品信息</a>
            <span class="gap">/</span>
            修改资料
        </div>

        {# 品牌头 #}
        <div class="brand_box">
            <label id="product_title">产品名称</label>:<br>
            <label id="product_name">{{ product.name }}</label>
        </div>


        <div class="container_box cell_layout">
            <div class="processor" id="process">
                <ol>
                    <li id="set1" class="current size1of1">
                        <div class="step_line"></div>
                        <div class="step_inner">
                            <i class="icon_step">1</i>
                            <h4>填写资料</h4>
                        </div>
                    </li>
                    <li id="set2" class="size1of2 last">
                        <div class="step_line"></div>
                        <div class="step_inner">
                            <i class="icon_step">2</i>
                            <h4>提交成功</h4>
                        </div>
                    </li>
                </ol>
            </div>
            <div class="step_panel text_padding">
                <div class="app_create_box app" id="step1">
                    <form class="form" id="form" novalidate="novalidate">
                        {% csrf_token %}
                        <div class="frm_control_group">
                            <label for="" class="frm_label">证书类型</label>
                            <div class="frm_controls">
                                <select id="type" class="frm_select_box" value="" name="type" lay-ignore>
                                    <option value ="" class="frm_select button">请选择证书类型</option>
                                    {% for type in license_types %}
                                        <option value ="{{ type.0 }}" class="frm_select button">{{ type.1 }}</option>
                                    {% endfor %}
                                </select>
                            </div>
                        </div>

                        <div class="frm_control_group" id="license_name">
                            <label for="" class="frm_label">证书名称</label>
                            <div class="frm_controls">
                                <span class="frm_input_box">
                                    <input name="name" id="name" class="frm_input" type="text">
                                </span>
                            </div>
                        </div>
                        <div id="name_upload" class="dynamic_box"></div>

                        <div class="frm_control_group">
                            <label for="" class="frm_label">证书生效期</label>
                            <div class="frm_controls">
                                <span class="frm_input_box">
                                    <input name="effect_time" id="effect_time" class="frm_input" type="text">
                                </span>
                            </div>
                        </div>
                        <div class="frm_control_group">
                            <label for="" class="frm_label">证书失效期</label>
                            <div class="frm_controls">
                                <span class="frm_input_box">
                                    <input name="expire_time" id="expire_time" class="frm_input" type="text">
                                </span>
                            </div>
                        </div>
                        <div class="frm_control_group"  id="license_img">
                            <label for="" class="frm_label">证书图片</label>
                            <div class="frm_controls">
                                <div class="frm_controls">
                                    <div class="upload_wrp">
                                        <div class="upload_box has_demo">
                                            <p class="upload_tips">
                                                请上传品牌证书图片
                                                <br>
                                                842*595像素，大小不超过500KB。
                                            </p>
                                            <button type="button" class="layui-btn" id="license" style="background-color: #44b549;">
                                                <i class="layui-icon">&#xe67c;</i>上传图片
                                            </button>
                                            <span class="upload_area webuploader-container"></span>
                                            <input name="img" id="license-img" value="" type="hidden">
                                            <p class="upload_preview size108" id="fileView-license" style="display: block;">
                                            </p>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>

                        <div class="tool_bar border with_form">
                            <a href="javascript:void(0);" class="btn btn_primary jsSendBt" id="brandBtn">提交</a>
                        </div>
                    </form>
{#                    <form action="/file-upload" class="dropzone" id="my-awesome-dropzone"></form>#}
                </div>

                <div class="app_create_box result" id="step2" style="display:none">
                    <div class="page_msg simple default">
                        <div class="inner group">
                            <span class="msg_icon_wrp">
                                <i class="icon_msg success"></i>
                            </span>
                            <div class="msg_content">
                                <h4>您的品牌证书已成功提交审核，预计在3天内完成审核</h4>
                                <p>
                                    审核通过后，你才可以申购溯码
                                </p>
                                <p class="spacing">
                                    <a href="{% url 'smErp:manage' %}" data-url="{% url 'smErp:manage' %}" target="_self" class="btn btn_primary jsUrlLink">查看管理中心</a>
                                </p>
                            </div>
                        </div>
                    </div>
                </div>

            </div>
        </div>
    </div>
{% endblock %}

{% block footscript %}
    <script src="{% static 'smErp/assets/dropzone/dropzone.min.js' %}"></script>
    <script>

        // 存储上传的图片
        var upload_imgs = {} ;
        var id = 1;
        $("#email").blur(function(){
{#           alert("hello world");#}
            var email = $("#email").val();
{#            alert(email);#}
            if(!email){
{#                alert("hellow");#}
                $("#email-msg").css("color","red");
                $("#email-msg").html("请输入邮箱地址");
                $("#email-msg").show();
            }
        });

        /*Dropzone.autoDiscover = false;

        $("#license_img").dropzone({
            url: "{% url 'smErp:image_upload' %}",
            addRemoveLinks: true,
            dictRemoveLinks: "x",
            dictCancelUpload: "x",
            dictRemoveFile:"删除",
            maxFiles: 1,
            maxFilesize: 5,
            acceptedFiles: "image/*",
            init: function() {
                this.on("success", function(file, data) {
                    console.log("File " + file.name + "uploaded");
                        var logo_obj = $("<input id='img' name='img' style='display:none;' value='" + data.img_url + "' readonly/>");
                        $("#license_img").append(logo_obj);
                        file.id = id;        // 换成服务端唯一的索引，可以后面删除对应的input
                        upload_imgs[file.id] = logo_obj;
                        id += 1;
                });
                this.on("removedfile", function(file) {
                    console.log("File " + file.name + "removed");
                    upload_imgs[file.id].remove();
                });
            }
        });
        */
        $("#brandBtn").on("click", function(){
            var name = $("#name").val();
            var type = $("#type").val();
            var effect_time = $("#effect_time").val();
            var expire_time = $("#expire_time").val();
            var img = $("#license-img").val();

            if(!name || !type || !effect_time || !expire_time ||!img){
                if(type == "4" && !name) {
                    //if (!name) {
                        layer.msg("请填写证书名称");
                        return;
                    //}
                }
                if(!type){
                    layer.msg("请选择证书类型");
                    return;
                }
                if(!effect_time){
                    layer.msg("请输入证书有效期");
                    return;
                }
                if(!expire_time){
                    layer.msg("请输入证书失效期");
                    return;
                }
                if(!img){
                    layer.msg("请上传证书图片");
                    return;
                }
            }

            var brand_license_info = $("#form").serialize();
            var params = $.param({'action': "create"}) + "&" + brand_license_info ;
            $.post("{% url 'smErp:product_license' product.id %}", params,
                    function(data, status, xhr) {
                        if(status == "success") {
                            if(data.result == 0) {
                                // 提交品牌信息
                                alert(brand_license_info);
                                // 切换状态
                                // 切换状态
                                $("#set2").addClass("current");
                                $("#set1").removeClass("current");
                                $("#set1").addClass("complete");
                                $("#set1").children("div[class='step_inner']").children("i[class='icon_step']").html("");
                                $("#set1").children(".step_line").css("background-color", "#44b549");
                                // 改变进度 栏状态
                                $("#step1").hide();
                                $("#step2").show();
                            }else{
                                layer.msg(data.reason);
                            }
                        }else{
                            layer.msg(status);
                        }

                    },
            "json");
        });

        layui.use(['laydate','upload'], function(){
            var laydate = layui.laydate;
            var upload = layui.upload;

            //执行一个laydate实例
            laydate.render({
                elem: '#effect_time', //指定元素
                theme: '#44b549'
            });

            laydate.render({
                elem: '#expire_time', //指定元素
                theme: '#44b549'
            });

            //证书图片上传
            var logo_uploadInst = upload.render({
                elem: '#license' //绑定元素
                ,url: "{% url 'smErp:image_upload' %}" //上传接口
                ,accept: 'images'
                ,data : {"max_size": 500, "height": 595, "width": 842}
                ,before: function(obj){ //obj参数包含的信息，跟 choose回调完全一致，可参见上文。
                    layer.load(); //上传loading
                }
                ,done: function(res){
                    //上传完毕回调
                    layer.closeAll('loading'); //关闭loading
                    if(res.result == 0){
                        // 上传成功，回显
                        layer.msg(res.reason);
                        var fileview_logo = $("#fileView-license");
                        fileview_logo.empty();
                        fileview_logo.append('<img id="" src="' + res.img_url + '">');
                        $("#license-img").val(res.img_url);

                    }else{
                        layer.msg(res.reason);
                    }
                }
                ,error: function(){
                    //请求异常回调
                    layer.closeAll('loading'); //关闭loading
                }
            });
        });

        // 证书类型的选择
        $("#type").bind("change",function(){
            var option = $(this).find('option:selected'); //获取选中的项
            console.log(option.val());
            if(option.val() == "4" || option.val() == ""){
                $("#license_name").show();
                $("#license_des").show();
                $("#license_icon").show();
            }else{
                $("#license_name").hide();
                $("#license_des").hide();
                $("#license_icon").hide();
            }
        });

    </script>
{% endblock %}




